<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  id="temName">
									<button class="btn btn-default" onclick="getData()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="temTable">
<!--			                          <tr>-->
<!--			                              <td><input  type="checkbox"></td>			                              -->
<!--				                          <td>1</td>-->
<!--									      <td>手机</td>-->
<!--									      <td>联想,华为,中兴</td>-->
<!--									      <td>网络制式</td>									 -->
<!--									      <td></td>		                                                                  -->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--									  <tr>-->
<!--			                              <td><input  type="checkbox"></td>			                              -->
<!--				                          <td>2</td>-->
<!--									      <td>服装</td>-->
<!--									      <td>七匹狼,雅戈尔</td>-->
<!--									      <td>服装尺码</td>									 -->
<!--									      <td></td>		                                                                  -->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型"id="name1" name="name">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brands" multiple style="width: 100%">
							<option th:each="brand : ${brand}" th:value="${brand.id}">[[${brand.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specs" multiple style="width: 100%">
							<option th:each="sf : ${sf}" th:value="${sf.id}">[[${sf.specName}]]</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性" id="insertTr"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px" id="table">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="insertTable">
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
<!--修改-->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <input type="hidden" name="id" id="temId">
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" placeholder="商品类型"id="name2" name="name">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brands1" multiple style="width: 100%">
								  <option th:each="brand : ${brand}" th:value="${brand.id}">[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="specs1" multiple style="width: 100%">
								  <option th:each="sf : ${sf}" th:value="${sf.id}">[[${sf.specName}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性" id="insertTr1"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px" id="table1">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="updateTable1">
<!--								  <tr>-->
<!--									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>-->
<!--									  <td><input class="form-control" placeholder="属性名称" ></td>-->
<!--									  <td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>-->
<!--								  </tr>-->
<!--								  <tr>-->
<!--									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>-->
<!--									  <td><input class="form-control" placeholder="属性名称" ></td>-->
<!--									  <td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>-->
<!--								  </tr>-->
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>

</html>
<script>
	$(function (){
		getData();
		$("#brands").select2();
		$("#specs").select2();
		$("#brands1").select2();
		$("#specs1").select2();
	})

	function getData(){
		$.ajax({
			url:"/templateController/selectTemplate",
			type:"post",
			dataType:"json",
			data:{temName:$("#temName").val()},
			success:function(result){
				var tbody="";
				var data = result.data;
				$(data).each(function(i,e){
					var specList = JSON.parse(e.specIds);
					var brandList = JSON.parse(e.brandIds);
					var customAttributeItemsList = JSON.parse(e.customAttributeItems);
					tbody += '<tr>';
					tbody += '<td><input type="checkbox" value="'+data[i].id+'"></td>';
					tbody += '<td>'+data[i].id+'</td>';
					tbody += '<td>'+data[i].name+'</td>';
					tbody += '<td>';
					$(specList).each(function (i,e) {
						if (i==specList.length-1){
							tbody +=e.text;
						}else {
							tbody +=e.text+",";
						}
					})
					tbody += '</td>';
					tbody += '<td>'
					$(brandList).each(function (i1,e1) {
						if (i1==brandList.length-1){
							tbody +=e1.text;
						}else {
							tbody += e1.text + ",";
						}
					})
					tbody += '</td>';
					tbody += '<td>'
					$(customAttributeItemsList).each(function (i2,e2) {
						if (i2==customAttributeItemsList.length-1){
							tbody +=e2.text;
						}else {
							tbody += e2.text+",";
						}
					})
					tbody += '</td>';
					tbody += ' <td class="text-center">';
					tbody += '<button type="button" onclick="getTempById('+data[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal1" >修改</button>';
					tbody += '</td></tr>';
				})
				$("#temTable").html(tbody);
			},
			error:function (result){
				alert("错误")
			}
		})
	}

	//表格尾部增加一行
	$(function(){
		$("#insertTr").click(function(){
			$("#table tbody").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
					'<td><input class="form-control" placeholder="属性名称" ></td>' +
					'<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function(){
		$("#table").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})

	//表格尾部增加一行
	$(function(){
		$("#insertTr1").click(function(){
			$("#table1 tbody").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
					'<td><input class="form-control" placeholder="属性名称" ></td>' +
					'<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function(){
		$("#table1").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})


	$(function(){
		$("#insertTable1").click(function(){
			$("#table1 tbody").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
					'<td><input class="form-control" placeholder="属性名称" ></td>' +
					'<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
		})
	})

	$(function(){
		$("#table1").on("click","btnDelete",function(){
			$(this).closest("tr").remove();
		})
	})

	function insert(){
		var name = $("#name1").val();
		var brands = $("#brands").select2('data');
		var arr = [];
		$(brands).each(function (i1,e1){
			var brand = {id:e1.id,text:e1.text.trim()};
			arr.push(brand);
		})
		var str = JSON.stringify(arr);
		var specs = $("#specs").select2('data');
		var arr1 = [];
		$(specs).each(function (i2,e2){
			var spec = {id:e2.id,text:e2.text};
			arr1.push(spec);
		})
		var str1 = JSON.stringify(arr1);
		var customList = [];
		var trs = $("#insertTable").find("tr");
		for (var i=0;i<trs.length;i++){
			var tds=$(trs[i]).find("td");
			var custom = $(tds).eq(1).find("input").val();
			var customs = {text:custom}
			customList.push(customs);
		}
		var json = JSON.stringify(customList);
		$.ajax({
			url:"/templateController/insertTemplate",
			type:"post",
			dataType:"json",
			data:{name:name,brandIds:str,specIds:str1,customAttributeItems:json},
			success:function(result){
				alert(result.message);
				location.reload();
			},
			error:function(result){
				alert("错误");
			}
		})
	}
	//回显
	function getTempById(id){
		$.ajax({
			url:"/templateController/getTemplateById?id="+id,
			type:"post",
			dataType:"json",
			data:{},
			success:function(result){
				var data = result.data;
				console.log(data)
				$("#temId").val(data.id);
				$("#name2").val(data.name);
				var brandList = JSON.parse(data.brandIds);
				var specIdList = JSON.parse(data.specIds);
				var cusList = JSON.parse(data.customAttributeItems);
				var brand = [];
				$(brandList).each(function(i,e){
					brand.push(e.id);
				})
				var spec = [];
				$(specIdList).each(function(i1,e1){
					spec.push(e1.id);
				})
				$("#brands1").val(brand).trigger("change");
				$("#specs1").val(spec).trigger("change");
				var tbody = "";
				for (var j=0;j<cusList.length;j++){
					tbody += '<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td>';
					tbody += '<td><input class="form-control" placeholder="属性名称" value='+ cusList[j].text+'></td>';
					tbody += '<td><button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td></tr>';
				}
				$("#updateTable1").html(tbody);
			},
			error:function (result){
				alert("错误");
			}
		})
	}
	//修改
	function updateInfo(){
		var temId = $("#temId").val();
		var temName = $("#name2").val();
		var brands = $("#brands1").select2("data");
		var specs = $("#specs1").select2("data");
		var cums = $("#updateTable1").find("tr");
		var arr = [];
		$(brands).each(function(i,e){
			var brand = {id:e.id,text:e.text}
			arr.push(brand);
		})
		var str = JSON.stringify(arr);
		var arr1 = [];
		$(specs).each(function(i1,e1){
			var spec = {id:e1.id,text:e1.text};
			arr1.push(spec);
		})
		var str2 = JSON.stringify(arr1);
		var arr3 = [];
		for (var j = 0;j<cums.length;j++){
			var tds=$(cums[j]).find("td");
			var custom = $(tds).eq(1).find("input").val();
			var customs = {text:custom}
			arr3.push(customs);
		}
		var str3 = JSON.stringify(arr3);
		$.ajax({
			url:"/templateController/updateTemplate",
			type:"post",
			dataType:"json",
			data:{id:temId,name:temName,specIds:str2,brandIds:str,customAttributeItems:str3},
			success:function (data){
				alert(data.message);
				location.reload();
			},
			error:function (data){
				alert("错误")
			}
		})

	}

	function deleteBatch(){
		var boxs = $("[type=checkbox]:checked");
		var ids = [];
		$(boxs).each(function (i,e){
			ids.push(e.value);
		})

		if (ids.length <= 0){
			alert("请选择要删除的行");
			return;
		}
		$.ajax({
			url:"/templateController/deleteBatch",
			type:"post",
			dataType:"json",
			data:{ids:ids},
			success:function(data){
				alert(data.message);
				location.reload();
			},
			error:function(data){
				alert("错误")
			}
		})
	}
</script>